/*
 * CPUINC.h
 *
 *  Created on: 2011-3-3
 *      Author: Dallas Cao
 */

#ifndef CPUINC_H_
#define CPUINC_H_

#ifndef TYPEDEF_H_
#include "../Typedef.h"
#endif

#define SET_FLAG(V)		{R.S |= (V);}
#define CLR_FLAG(V)		{R.S &= ~(V);}

//<<=========================Zero-page Indexed===============================>>
#define ZPRD(A)			(RAM[(BYTE)(A)])
#define ZPRDW(A)		(WORD)(ZPRD(A)+(ZPRD(A+1)<<8))
#define ZPWR(A,V)		{RAM[(BYTE)(A)] = (V);}
#define ZPWRW(A,V)		{ZPWR(A,(BYTE)(V)); ZPWR(A+1,(BYTE)(V>>8));}
//<<=========================Zero-page Indexed===============================>>

//<<=========================ADDRESSING MODES================================>>

// WT(WORD) .... WORD TEMP
// EA(WORD) .... EFFECTIVE ADDRESS
// ET(WORD) .... EFFECTIVE ADDRESS TEMP
// DT(BYTE) .... DATA


/* Immediate */
#define AM_IM()	{				\
	DT = Read(R.PC++);			\
}

/* Absolute */
#define AM_AB()	{				\
	EA = ReadW(R.PC);			\
	DT = Read(EA);				\
	R.PC += 2;					\
}

/* Absolute,X */
#define AM_AX()	{				\
	ET = ReadW(R.PC);			\
	EA = ET+(WORD)R.X;			\
	DT = Read(EA);				\
	R.PC += 2;					\
}

/* Absolute,Y */
#define AM_AY()	{				\
	ET = ReadW(R.PC);			\
	EA = ET+(WORD)R.Y;			\
	DT = Read(EA);				\
	R.PC += 2;					\
}

/* Zero-page */
#define AM_ZP()	{				\
	EA = Read(R.PC++);			\
	DT = ZPRD(EA);				\
}

/* Zero Page,X */
#define AM_ZX()	{				\
	DT = Read(R.PC++);			\
	EA = (BYTE)(DT+R.X);		\
	DT = ZPRD(EA);				\
}

/* Zero Page,Y */
#define AM_ZY()	{				\
	DT = Read(R.PC++);			\
	EA = (BYTE)(DT+R.Y);		\
	DT = ZPRD(EA);				\
}

/* 先变址X 后间接寻址 */
#define AM_IX()	{				\
	DT = Read(R.PC++);			\
	EA = ZPRDW(DT+R.X);			\
	DT = Read(EA);				\
}

/* 后变址Y 间接寻址 */
#define AM_IY()	{				\
	DT = Read(R.PC++);			\
	ET = ZPRDW(DT);				\
	EA = ET+(WORD)R.Y;			\
	DT = Read(EA);				\
}

/* =====EFFECTIVE ADDRESS===== */
#define EA_AB()	{				\
	EA = ReadW(R.PC);			\
	R.PC += 2;					\
}

#define EA_AX()	{				\
	ET = ReadW(R.PC);			\
	EA = ET+(WORD)R.X;			\
	R.PC += 2;					\
}

#define EA_AY()	{				\
	ET = ReadW(R.PC);			\
	EA = ET+(WORD)R.T;			\
	R.PC += 2;					\
}

#define EA_ZP()	{				\
	EA = ReadW(R.PC);			\
	R.PC += 2;					\
}

#define EA_ZX()	{				\
	ET = ReadW(R.PC);			\
	EA = (BYTE)(ET+R.X);		\
	R.PC += 2;					\
}

#define EA_ZY()	{				\
	ET = ReadW(R.PC);			\
	EA = (BYTE)(ET+R.Y);		\
	R.PC += 2;					\
}

/* 先变址X 后间接寻址 */
#define EA_IX()	{				\
	DT = Read(R.PC++);			\
	EA = ZPRDW(DT+R.X);			\
}

/* 后变址Y 间接寻址 */
#define EA_IY()	{				\
	DT = Read(R.PC++);			\
	ET = ZPRDW(DT);				\
	EA = ET+(WORD)R.Y;			\
}

//<<=========================ADDRESSING MODES================================>>

/* ADC (N   V       B   D   I   Z   C) */
#define ADC(){}


#endif /* CPUINC_H_ */
